import numpy as np


def change_nan_mean(t1):
    for i in range(t1.shape[1]):  # 遍历每一列
        temp_col = t1[:, i]  # 当前列一列
        nan_num = np.count_nonzero(temp_col != temp_col)  # nan的数量
        if nan_num != 0:
            temp_not_nan_col = temp_col[temp_col == temp_col]  # 当前列中除去nan组成数组

            # 将选中当前为nan的位置,把值赋值为不为nan的均值(当前nan在列方向只有一个)
            temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
    return t1


if __name__ == '__main__':
    t1 = np.arange(24).reshape((4, 6)).astype('float')  # nan为浮点数,当前为int32,转为float才能赋值为nan
    t1[1, 2:] = np.nan
    print(t1)
    print('*'*50)
    t1 = change_nan_mean(t1)
    print(t1)
